技术经验 • dingxiao • 阅读数:5826 • 2020年2月6日 13:03
0x01-驱动ST7735显示芯片
实在无聊将原先买的一块0.96寸的tft液晶显示模块拿来玩,该显示模块驱动芯片为ST7735。
首先想到的是U8G2是否支持该驱动芯片,查找资料发现U8G2并不支持该芯片,故放弃。
0x02-好资料
在找资料的过程中找到一篇好的资料:https://www.instructables.com/id/Select-Color-Display-for-ESP32/,该较为详细的介绍了blog主用ESP32作为主控驱动过的显示模块。
0x03-TFT_eSPI成功驱动ST7735
最终选择Adafruit_GFX 库的改进版来驱动ST7735,并且按照示例程序成功驱动。
库地址:https://github.com/XTronical/Adafruit-ST7735-Library
硬件连接关系为:
#define TFT_DC 21
#define TFT_RST 22
#define TFT_CS 4
#define TFT_SCL 18
#define TFT_SDA 23
示例程序为:
#include <Adafruit_GFX.h> // Core graphics library
#include "XTronical_ST7735.h" // Hardware-specific library
#include <SPI.h>
// set up pins we are going to use to talk to the screen
#define TFT_DC 21 // register select (stands for Data Control perhaps!)
#define TFT_RST 22 // Display reset pin, you can also connect this to the ESP32 reset
// in which case, set this #define pin to -1!
#define TFT_CS 4 // Display enable (Chip select), if not enabled will not talk on SPI bus
// initialise the routine to talk to this display with these pin connections (as we've missed off
// TFT_SCLK and TFT_MOSI the routine presumes we are using hardware SPI and internally uses 13 and 11
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
float p = 3.1415926;
void setup(void) {
tft.init(); // initialize a ST7735S chip,
}
void loop() {
// for(uint16_t rotation=0; rotation<4;rotation++)
// Demo(rotation);
Demo(3);
}
void Demo(uint16_t rotation)
{
tft.setRotation(rotation);
uint16_t time = millis();
tft.fillScreen(ST7735_BLACK);
time = millis() - time;
delay(500);
// large block of text
tft.fillScreen(ST7735_BLACK);
testdrawtext("This is a test of some very log text that I written for the sole purpose of checking if the display displays very long text correctly.", ST7735_WHITE);
delay(1000);
// tft print function!
tftPrintTest();
delay(4000);
// a single pixel
tft.drawPixel(tft.width()/2, tft.height()/2, ST7735_GREEN);
delay(500);
testdrawrects(ST7735_GREEN);
delay(500);
testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
delay(500);
tft.fillScreen(ST7735_BLACK);
testfillcircles(10, ST7735_BLUE);
testdrawcircles(10, ST7735_WHITE);
delay(500);
testroundrects();
delay(500);
testtriangles();
delay(1000);
for(int i=2;i>0;i--)
{
tft.invertDisplay(true);
delay(500);
tft.invertDisplay(false);
delay(500);
}
}
void testdrawtext(char *text, uint16_t color) {
tft.setCursor(0,0);
tft.setTextColor(color);
tft.setTextWrap(true);
tft.print(text);
}
void testdrawrects(uint16_t color) {
tft.fillScreen(ST7735_BLACK);
for (int16_t x=0; x < tft.width(); x+=6) {
tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
}
}
void tftPrintTest() {
tft.setTextWrap(false);
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextColor(ST7735_YELLOW);
tft.setTextSize(2);
tft.println("I'm running");
tft.setTextColor(ST7735_GREEN);
tft.setTextSize(3);
tft.println("on a");
tft.setTextColor(ST7735_BLUE);
tft.println("ESP32");
tft.setTextSize(4);
tft.setTextColor(ST7735_CYAN);
tft.print("Ha ha!");
delay(1500);
tft.setCursor(0, 0);
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(0);
tft.println("Hello World!");
tft.setTextSize(1);
tft.setTextColor(ST7735_GREEN);
tft.print(p, 6);
tft.println(" Want pi?");
tft.println(" ");
tft.print(8675309, HEX); // print 8,675,309 out in HEX!
tft.println(" Print HEX!");
tft.println(" ");
tft.setTextColor(ST7735_WHITE);
tft.println("Sketch has been");
tft.println("running for: ");
tft.setTextColor(ST7735_MAGENTA);
tft.print(millis() / 1000);
tft.setTextColor(ST7735_WHITE);
tft.print(" seconds.");
}
void mediabuttons() {
// play
tft.fillScreen(ST7735_BLACK);
tft.fillRoundRect(25, 10, 78, 60, 8, ST7735_WHITE);
tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_RED);
delay(500);
// pause
tft.fillRoundRect(25, 90, 78, 60, 8, ST7735_WHITE);
tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_GREEN);
tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_GREEN);
delay(500);
// play color
tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_BLUE);
delay(50);
// pause color
tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_RED);
tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_RED);
// play color
tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_GREEN);
}
void testfillrects(uint16_t color1, uint16_t color2) {
tft.fillScreen(ST7735_BLACK);
for (int16_t x=tft.width()-1; x > 6; x-=6) {
tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
}
}
void testfillcircles(uint8_t radius, uint16_t color) {
for (int16_t x=radius; x < tft.width(); x+=radius*2) {
for (int16_t y=radius; y < tft.height(); y+=radius*2) {
tft.fillCircle(x, y, radius, color);
}
}
}
void testdrawcircles(uint8_t radius, uint16_t color) {
for (int16_t x=0; x < tft.width()+radius; x+=radius*2) {
for (int16_t y=0; y < tft.height()+radius; y+=radius*2) {
tft.drawCircle(x, y, radius, color);
}
}
}
void testtriangles() {
tft.fillScreen(ST7735_BLACK);
int color = 0xF800;
int t;
int w = tft.width()/2;
int x = tft.height()-1;
int y = 0;
int z = tft.width();
for(t = 0 ; t <= 15; t++) {
tft.drawTriangle(w, y, y, x, z, x, color);
x-=4;
y+=4;
z-=4;
color+=100;
}
}
void testroundrects() {
tft.fillScreen(ST7735_BLACK);
int color = 100;
int i;
int t;
for(t = 0 ; t <= 4; t+=1) {
int x = 0;
int y = 0;
int w = tft.width()-2;
int h = tft.height()-2;
for(i = 0 ; i <= 16; i+=1) {
tft.drawRoundRect(x, y, w, h, 5, color);
x+=2;
y+=3;
w-=4;
h-=6;
color+=1100;
}
color+=100;
}
}
0x04-注意点
需要注意的是在用该库驱动ST7735时,需要对库的用户配置头文件进行修改,主要修改内容为TFT显示的宽和高的尺寸。
最终驱动成功图